﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Linq;

using MultiGed2.Topologie;
using MultiGed2.Provoz;
using MultiGed2.Properties;

namespace MultiGed2
{
 public partial class MainForm : Form
 {
  Oblast oblast;
  TypOblasti typObl = TypOblasti.None;
  List<Dopravna> dopHledaniTrasy;

  public MainForm()
  {
   InitializeComponent();
   object posX = Utils.ReadRegistryValue(Utils.regPosX);
   object posY = Utils.ReadRegistryValue(Utils.regPosY);
   if (posX != null && posY != null)
    this.Location = new Point((int)posX, (int)posY);

   cbRychlost.VybranaRychlost = Rychlost.V120;
   cbTypVlaku.SelectedIndex = 1;

   object oblIndex = Utils.ReadRegistryValue(Utils.regOblast);
   if (oblIndex != null)
    cbOblast.SelectedIndex = (int)oblIndex;
  }

  private void btnInitOblast_Click(object sender, EventArgs e)
  {
  }

  private void btnNajitTrasu_Click(object sender, EventArgs e)
  {
   if (oblast == null)
   {
    eventLog.LogInfo(Resources.sLoadAreaFirst);
    return;
   }
   if (cbdVychozi.VybranaDopravna != null && cbdCilova.VybranaDopravna != null &&
    cbTypVlaku.SelectedIndex >= 0)
   {
    Rychlost rychl = cbRychlost.VybranaRychlost;

    VlDruhJD typvlak = VlDruhJD.R;
    switch (cbTypVlaku.SelectedIndex)
    {
     case 0: typvlak = VlDruhJD.RN; break;
     case 1: typvlak = VlDruhJD.R; break;
     case 2: typvlak = VlDruhJD.Os; break;
     case 3: typvlak = VlDruhJD.Nakl; break;
     case 4: typvlak = VlDruhJD.Lv; break;
    }

    txtHledaniTras.Text = "";

    List<List<Dopravna>> trasy = null;
    if (cbdOper.VybranaDopravna == null)
    {
     trasy = Hledani.TrasaZDo(cbdVychozi.VybranaDopravna,
      cbdCilova.VybranaDopravna, oblast);
    }
    else
    {
     txtHledaniTras.Text = "N/A";
     return;
    }

    // vypis vysledku
    txtHledaniTras.Text = String.Format("{0}: {1}\r\n", Resources.sFoundRoutesCount, trasy.Count);
    foreach (List<Dopravna> trasa in trasy)
    {
     Dopravna predchozi = trasa[0];
     byte jd = 0;
     string s = predchozi.zkratka;
     foreach (Dopravna d in trasa.Skip(1))
     {
      jd = oblast.najdiJizdniDobu(predchozi, d, typvlak, rychl);
      s = s + String.Format(" - {0}' - {1}", jd, d.zkratka);
      predchozi = d;
     }
     txtHledaniTras.Text += s + "\r\n";
    }
   }
   else
   {
    eventLog.LogInfo(Resources.sMissingFindParams);
   }
  }

  private void btnProhoz_Click(object sender, EventArgs e)
  {
   Dopravna tmp = cbdCilova.VybranaDopravna;
   cbdCilova.VybranaDopravna = cbdVychozi.VybranaDopravna;
   cbdVychozi.VybranaDopravna = tmp;
  }

  private void oznameniChybyNacitani(Vlak v, StavNacitani s)
  {
   eventLog.LogWarning(String.Format(Resources.sParseTrainError, v.cisloVlaku, s));
  }

  private void oznameniChybyKonzistence(Vlak v, ChybaKontrolyDat ch)
  {
   eventLog.LogWarning(String.Format(Resources.sCheckTrainError, v.cisloVlaku, ch));
  }

  List<Vlak> vlaky;

  private void btnOtevrGvd_Click(object sender, EventArgs e)
  {
  }

  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
   Utils.WriteRegistryValue(Utils.regPosX, this.Location.X);
   Utils.WriteRegistryValue(Utils.regPosY, this.Location.Y);
  }

  private void btnLoadGVD_Click(object sender, EventArgs e)
  {
   txtHledaniTras.Text = "";
   vlakList.SmazatSeznam();
   typObl = cbOblast.VybranaOblast;
   SimulacniOblast simOblast = new SimulacniOblast(typObl);
   oblast = simOblast.oblast;
   if (oblast == null)
   {
    eventLog.LogError(String.Format(Resources.sLoadAreaFailed, typObl));
   }
   else
   {
    // init oblasti
    eventLog.LogSuccess(String.Format(Resources.sLoadAreaOK, typObl));
    Utils.WriteRegistryValue(Utils.regOblast, cbOblast.SelectedIndex);
    dopHledaniTrasy = oblast.dopravny.Where(
     d => d.kategorie != KategorieDopravny.Pomocna).ToList();
    cbdVychozi.NastavDopravny(dopHledaniTrasy);
    cbdCilova.NastavDopravny(dopHledaniTrasy);
    cbdOper.NastavDopravny(dopHledaniTrasy);

    // nacteni vlaku
    object adresar = Utils.ReadRegistryValue(Utils.regLastDir),
    soubor = Utils.ReadRegistryValue(Utils.regLastFile);
    if (adresar != null)
     ofdOtevrGvd.InitialDirectory = (string)adresar;
    if (soubor != null)
     ofdOtevrGvd.FileName = (string)soubor;
    ofdOtevrGvd.Title = Resources.sOpenGvdPrompt + cbOblast.SelectedItem;
    DialogResult dr = ofdOtevrGvd.ShowDialog();
    if (dr == DialogResult.OK)
    {
     Cursor.Current = Cursors.WaitCursor;

     string nazev = ofdOtevrGvd.FileName;
     short failedRead, failedCheck;

     vlaky = Utils.vlakyZeSouboru(nazev, oblast, oznameniChybyNacitani, out failedRead);
     FileInfo fi = new FileInfo(nazev);
     Utils.WriteRegistryValue(Utils.regLastDir, fi.Directory.FullName);
     Utils.WriteRegistryValue(Utils.regLastFile, fi.Name);

     Utils.kontrolaDatGVD(vlaky, oblast, oznameniChybyKonzistence, out failedCheck);

     // plneni seznamu vlaku
     vlakList.PridejVlaky(vlaky);

     string loadResult = String.Format("{0} ({1} OK, {2} KO, {3} OUT)",
      Resources.sLoadedTrainsCount, vlaky.Count, failedRead, failedCheck);
     if (vlaky.Count == 0)
      eventLog.LogError(loadResult);
     else if (failedRead + failedCheck == 0)
      eventLog.LogSuccess(loadResult);
     else
      eventLog.LogWarning(loadResult);

     Cursor.Current = Cursors.Default;
    }
   }
  }
 }
}
